package com.lsf.action;

import java.util.ArrayList;
import java.util.List;

import java.util.Map;

import com.lsf.po.PubMessage;
import com.lsf.service.PubMessageService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**

 * 创建日期：2013-3-10上午11:20:09

 * 修改日期：

 * 作者：刘帅飞

 */

public class LoadPubMessagesAction extends ActionSupport{
	private List<PubMessage> loadPubMessages;
	private PubMessageService pubMessageService;	//用户业务接口引用  （3）userService通过spring容器（applicationContext.xml）初始化
	/*pubMessageService的getter和的setter方法*/
	public void setPubMessageService(PubMessageService pubMessageService) {
		this.pubMessageService = pubMessageService;
	}	
	/*loadPubMessages的getter和的setter方法*/
	public void setLoadPubMessages(List<PubMessage> loadPubMessages) {
		this.loadPubMessages = loadPubMessages;
	}
	public List<PubMessage> getLoadPubMessages() {
		return loadPubMessages;
	}	
	
	
	public String execute() throws Exception {	
				
		/*获得session对象*/
		 ActionContext actionContext = ActionContext.getContext();
	     Map session = actionContext.getSession();
	     
	   /* 读取session内所存的最大的messageId即theBiggestMessageId*/
		int end = (Integer)session.get("theBiggestMessageId");
		System.out.println("first=  "+end);
		
		List<PubMessage> developedPubMessages = new ArrayList<PubMessage>();
		/*获得5秒内所有PubMessage所在list：“loadPubMessages”*/	
		loadPubMessages = pubMessageService.findAllPubMessage(0);
	
		/*判断名为loadPubMessages的list内的元素PubMessage的messageId是否大于theBiggestMessageId，如果大于
		则将此元素添加入名为developedPubMessages的list内*/
		for(PubMessage item : loadPubMessages){			
			if(item.getMessageId()>end){
				System.out.println("MessageId=  "+item.getMessageId());
			developedPubMessages.add(item);//developedPubMessages为最终要返回现实的list
			}
		}
		
		/*获得loadPubMessages这个list内所存的PubMessage的最大MessageId为theBiggestMessageId*/
		int lastOne = loadPubMessages.size()-1;		
		if(lastOne!=-1){//即loadPubMessages不为空时
			int theBiggestMessageId = loadPubMessages.get(lastOne).getMessageId();		
			/*获得session对象，存取最大MessageId*/
			//session.put("theBiggestMessageId",Integer.toString(theBiggestMessageId));	
			session.put("theBiggestMessageId",theBiggestMessageId);
			System.out.println("second=  "+theBiggestMessageId);
		}
		loadPubMessages=developedPubMessages;
		System.out.println("third=  "+session.get("theBiggestMessageId"));
		return SUCCESS;		
	}
}		




